// Android MNet runtime.
//
// Copyright 2011 Martin Leidel, all rights reserved.
//
// This software is provided 'as-is', without any express or implied
// warranty.  In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
// 3. This notice may not be removed or altered from any source distribution.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

class mnet
{
  public static String result = "";

  public static String getResult()
  {
    return result;
  }

  public static String asyncHttpGet( String url, int timeoutConnection, int timeoutSocket )
  {
    result = "";
    httpThread ht = new httpThread( url, timeoutConnection, timeoutSocket );
    ht.start();
    return "started";
  }

  public static String HttpGet( String url, int timeoutConnection, int timeoutSocket )
  {
    HttpURLConnection con = null;
    InputStream is = null;
    try 
    {
      URL connectToURL = new URL( url );
      con = ( HttpURLConnection )connectToURL.openConnection();
      con.setReadTimeout( timeoutSocket );
      con.setConnectTimeout( timeoutConnection );
      con.setRequestMethod( "GET" );
      con.setDoInput( true );
      // Start the query
      con.connect();
      is = con.getInputStream();
    }
    catch( IOException e )
    {
      mnet.result = "ERR_HttpGet " + e.getMessage();
    }
    return convertStreamToString( is );
  }

  public static String convertStreamToString( InputStream is )
  {
    StringBuilder sb = new StringBuilder();
    try
    {
      BufferedReader reader = new BufferedReader( new InputStreamReader( is ) );
      String line = null;
      while( ( line = reader.readLine() ) != null )
      {
        sb.append( line + "\n" );
      }
      is.close();
    }
    catch( IOException e )
    {
      mnet.result = "ERR_convertStreamToString " + e.getMessage();
    }
    return sb.toString();
  }
}

class httpThread extends Thread
{
  String url;
  int timeoutConnection;
  int timeoutSocket;

  httpThread( String url, int timeoutConnection, int timeoutSocket )
  {
    this.url = url;
    this.timeoutConnection = timeoutConnection;
    this.timeoutSocket = timeoutSocket;
  }

  public void run()
  {
    // System.out.println(mnet.asyncHttpGet(url, timeoutConnection, timeoutSocket));
    try
    {
       mnet.result = mnet.HttpGet( url, timeoutConnection, timeoutSocket );
    }
    catch( Exception e )
    {
       mnet.result = "ERR_run " + e.getMessage();
    }
  }
}
